package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuanwei.core.cache.HomePageCache;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwBannerService;
import com.xuanwei.dao.entity.XwBanner;
import com.xuanwei.dao.mapper.XwBannerMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-06-23
 */
@Service
public class XwBannerServiceImpl extends ServiceImpl<XwBannerMapper, XwBanner> implements IXwBannerService {



    @Override
    public void add(XwBanner banner) {
        banner.setUpdateTime(LocalDateTime.now());
        if(banner.getId()==null){
            banner.setCreateTime(LocalDateTime.now());
            save(banner);
        }else {
            XwBanner byId = getById(banner.getId());
            if(byId==null){
                throw new XwException("该数据不存在");
            }
            updateById(banner);
        }
    }

    @Override
    public void changeStatus(Integer id) {
        XwBanner banner = getById(id);
        if(banner==null){
            throw new XwException("该数据不存在");
        }
        banner.setUpdateTime(LocalDateTime.now());
        banner.setShowStatus(banner.getShowStatus()==0?1:0);
        updateById(banner);
    }

    @Override
    public List<XwBanner> findAll() {

        return list(new QueryWrapper<XwBanner>().select("id,`name`,`show_status`,image_url,link_url,create_time"));

    }
}
